function GetPluginTypeInstalled() {
    var anyVersionInstalled = navigator.mimeTypes && navigator.mimeTypes["application/x-sharepoint"] && navigator.mimeTypes["application/x-sharepoint"].enabledPlugin;
    if (anyVersionInstalled) {
        if (navigator.mimeTypes["application/x-sharepoint"].enabledPlugin.filename == 'NPSPWRAP.DLL')
        {
            return "MS";
        }
        else if (navigator.mimeTypes["application/x-sharepoint"].enabledPlugin.filename == 'npOpenDocuments.dll') {
            return "RENIT";
        }
    } 
    return "NONE";
}

function InstallOpenDocumentsPluginIfMissing() {
    var ff = /Firefox/.exec(navigator.userAgent);
    var chrome = /Chrome/.exec(navigator.userAgent);
    var typeInstalled = GetPluginTypeInstalled();

    if (ff && typeInstalled == "NONE") {
        var params = {
            "Firefox Support for SharePoint": { URL: npOpenDocumentsPluginRoot + 'npOpenDocuments.v141.xpi',
                toString: function () { return this.URL; }
            }
        };
        InstallTrigger.install(params);
    } else if (chrome && typeInstalled != "RENIT") {
        window.location.href = npOpenDocumentsPluginRoot + 'npOpenDocuments.v141.crx';
    }
}

function OverrideOpenDocumentScripts() {
    if (browseris.nav6up) {
        var obj = document.createElement("embed");
        obj.type = "application/x-sharepoint";
        obj.setAttribute("hidden", "true");
        obj.id = "wssIntegration";
        document.body.appendChild(obj);

        StsOpenEnsureEx = function() {
            return obj;
        };

        ActiveXObject = function(progid) {
            return new StsOpenEnsureEx();
        };

        window.DispDocItemEx = function(ele, fTransformServiceOn, fShouldTransformExtension, fTransformHandleUrl, strProgId) {
            itemTable = FindSTSMenuTable(ele, "CTXName");
            var stsOpen;
            var szHref;
            var szExt;
            var fRet = true;
            var szFileType = itemTable != null ? GetAttributeFromItemTable(itemTable, "Ext", "FileType") : "";
            var szAppId = "";
            var tblFileDlg = document.getElementById("FileDialogViewTable");
            if (tblFileDlg != null) {
                return true;
            }
            szHref = itemTable != null ? GetAttributeFromItemTable(itemTable, "Url", "ServerUrl") : "";
            if (szHref == null || szHref == "")
                szHref = ele.href;
            else
                szHref = SzServer(ele.href) + szHref;
            szExt = SzExtension(szHref);
            if ((currentItemProgId == null) && (itemTable != null))
                currentItemProgId = itemTable.getAttribute("HTMLType");
            if (currentItemProgId != null)
                szAppId = currentItemProgId;
            if (FDefaultOpenForReadOnly(szExt)) {
                if (strProgId.indexOf("SharePoint.OpenDocuments") >= 0)
                    strProgId = "SharePoint.OpenDocuments.3";
            }
            else if (!FSupportCheckoutToLocal(szExt)) {
                strProgId = "";
            }
            if ((currentItemCheckedOutUserId == null) && (itemTable != null))
                currentItemCheckedOutUserId = itemTable.COUId;
            if ((currentItemCheckedoutToLocal == null) && (itemTable != null))
                currentItemCheckedoutToLocal = GetAttributeFromItemTable(itemTable, "COut", "IsCheckedoutToLocal ");
            if (((currentItemCheckedOutUserId != null) &&
		(currentItemCheckedOutUserId != "") &&
		(currentItemCheckedOutUserId == ctx.CurrentUserId) &&
		(strProgId == "" || ((strProgId.indexOf("SharePoint.OpenDocuments")) >= 0)) &&
		FSupportCheckoutToLocal(szExt)) ||
		(strProgId == "SharePoint.OpenDocuments")) {
                strProgId = "SharePoint.OpenDocuments.3";
            }
            var stsopenVersion = 2;
            if (strProgId != '' && HasRights(0x10, 0x0)) {
                if ((strProgId.indexOf(".3")) >= 0)
                    stsopenVersion = 3;
                stsOpen = StsOpenEnsureEx(strProgId);
                if (stsOpen == null && stsopenVersion == 3) {
                    strProgId = strProgId.replace(".3", ".2");
                    stsOpen = StsOpenEnsureEx(strProgId);
                    stsopenVersion = 2;
                }
            }
            if (stsOpen != null) {
                if (stsopenVersion == 2 ||
			((itemTable == null) && (currentItemCheckedOutUserId == null)) ||
			(ctx.isVersions == 1 && (itemTable == null || itemTable.isMostCur == "0"))) {
                    try {
                        if ((currentItemCheckedOutUserId != null) &&
					(currentItemCheckedOutUserId != "") &&
					(currentItemCheckedOutUserId == ctx.CurrentUserId ||
					ctx.CurrentUserId == null)) {
                            if (currentItemCheckedoutToLocal == '1') {
                                alert(L_OpenDocumentLocalError_Text);
                                fRet = false;
                            }
                            else
                                fRet = stsOpen.EditDocument2(window, szHref, szAppId);
                        }
                        else {
                            fRet = stsOpen.ViewDocument2(window, szHref, szAppId);
                        }
                    }
                    catch (e) {
                        fRet = false;
                    }
                    if (fRet)
                        window.onfocus = RefreshOnNextFocus;
                }
                else {
                    var iOpenFlag = 0;
                    if (currentItemCheckedOutUserId != "") {
                        if ((currentItemCheckedOutUserId != ctx.CurrentUserId) && (ctx.CurrentUserId != null))
                            iOpenFlag = 1;
                        else if (currentItemCheckedoutToLocal == null ||
					currentItemCheckedoutToLocal != '1')
                            iOpenFlag = 2;
                        else
                            iOpenFlag = 4;
                    }
                    else if (!HasRights(0x0, 0x4) || FDefaultOpenForReadOnly(szExt))
                        iOpenFlag = 1;
                    else if (ctx.isForceCheckout == true)
                        iOpenFlag = 3;
                    try {
                        fRet = stsOpen.ViewDocument3(window, szHref, iOpenFlag, szAppId);
                    }
                    catch (e) {
                        fRet = false;
                    }
                    if (fRet) {
                        var fRefreshOnNextFocus = stsOpen.PromptedOnLastOpen();
                        if (fRefreshOnNextFocus)
                            window.onfocus = RefreshOnNextFocus;
                        else
                            SetWindowRefreshOnFocus();
                    }
                }
            }
            else if (currentItemCheckedoutToLocal == '1') {
                alert(L_OpenDocumentLocalError_Text);
            }
            if (stsOpen == null || !fRet) {
                if (fTransformServiceOn == 'TRUE' &&
			fShouldTransformExtension == 'TRUE' &&
			fTransformHandleUrl == 'TRUE') {
                    if (itemTable == null)
                        return fRet;
                    var getHttpRoot = new Function("return " + itemTable.getAttribute("CTXName") + ".HttpRoot;");
                    GoToPage(getHttpRoot() + "/_layouts" + "/htmltrverify.aspx?doc=" + escapeProperly(szHref));
                }
                return;
            }
            stsOpen = null;
            return !fRet;
        };
    }
}